package com.sword.controller.stock;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.stock.product.Productclass;
import com.sword.pojo.stock.product.Productlist;
import com.sword.pojo.stock.product.Productunit;
import com.sword.pojo.system.Employee;
import com.sword.service.stock.product.ProductClassService;
import com.sword.service.stock.product.ProductListService;
import com.sword.service.stock.product.ProductUnitService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.Console;
import java.security.Principal;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/product")
public class CategoriesController {
	
	/**
	 * 注入service
	 *
	 * @author <1254087415@qq.com>
	 * @since 2021/6/4 13:12
	 */
	@Autowired
	private ProductClassService productClassService;
	
	@Autowired
	private ProductListService productListService;
	
	@Autowired
	private ProductUnitService productUnitService;
	
	/**
	 * 商品总分类，细分类，计量单位分类渲染
	 *
	 * @return java.lang.String
	 * @author <1254087415@qq.com>
	 * @since 2021/6/4 0:33
	 */
	@RequestMapping("/productClass")
	public String productClass() {
		return "views/stock/productclass";
	}
	
	@RequestMapping("/productList")
	public String productList() {
		return "views/stock/productlist";
	}
	
	@RequestMapping("/productUnit")
	public String productUnit() {
		return "views/stock/productunit";
	}
	
	
	/**
	 * 查询商品总分类列表功能
	 *
	 * @param queryPageBean 分页查询类
	 * @return com.sword.page.PageResult
	 * @author <1254087415@qq.com>
	 * @since 2021/6/4 0:35
	 */
	@RequestMapping("/queryProductClass")
	@ResponseBody
	public PageResult queryProductClass(@RequestBody QueryPageBean queryPageBean) {
		try {
			return productClassService.queryProductClass(queryPageBean);
		} catch (Exception e) {
			e.printStackTrace();
			return new PageResult(500, MessageConstant.QUERY_PRODOCTCLASS_FAIL, null, null);
		}
	}
	
	/**
	 * 添加商品总分类功能
	 *
	 * @param productclass 商品总分类数据
	 * @return com.sword.page.Result
	 * @author <1254087415@qq.com>
	 * @since 2021/6/5 11:16
	 */
	@ResponseBody
	@RequestMapping("/addProductClass")
	public Result addProductClass(@RequestBody Productclass productclass) {
		try   {
			// 获取session保存进数据
			String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
			productclass.setEmployeeid(userId);
			
			// 判断是否添加成功
			Integer flag = productClassService.addProductClass(productclass);
			if (flag <= 0) {
				return new Result(500, MessageConstant.ADD_PRODOCTCLASS_FAIL, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(500, MessageConstant.ADD_PRODOCTCLASS_FAIL, null);
		}
		return new Result(200, MessageConstant.ADD_PRODOCTCLASS_SUCCESS, null);
	}
	
	/**
	 * 编辑商品总分类功能
	 *
	 * @param productclass 商品总分类数据
	 * @return com.sword.page.Result
	 * @author <1254087415@qq.com>
	 * @since 2021/6/5 11:26
	 */
	@ResponseBody
	@RequestMapping("editProductClass")
	public Result editProductClass(@RequestBody Productclass productclass) {
		try {
			Integer flag = productClassService.editProductClass(productclass);
			if (flag <= 0) {
				return new Result(500, MessageConstant.EDIT_PRODOCTCLASS_FAIL, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(500, MessageConstant.EDIT_PRODOCTCLASS_FAIL, null);
		}
		return new Result(200, MessageConstant.EDIT_PRODOCTCLASS_SUCCESS, null);
	}
	
	/**
	 * 删除商品总分类
	 *
	 * @param id 商品总分类id
	 * @return com.sword.page.Result
	 * @author <1254087415@qq.com>
	 * @since 2021/6/5 11:37
	 */
	@ResponseBody
	@RequestMapping("deleteProductClass")
	public Result deleteProductClass(Integer id) {
		try {
			Integer flag = productClassService.deleteProductClass(id);
			if (flag <= 0) {
				return new Result(500, MessageConstant.DELETE_PRODOCTCLASS_FAIL, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(500, MessageConstant.DELETE_PRODOCTCLASS_FAIL, null);
		}
		return new Result(200, MessageConstant.DELETE_PRODOCTCLASS_SUCCESS, null);
	}
	
	/**
	 * 查询所有商品总分类id
	 *
	 * @return com.sword.page.Result
	 * @author <1254087415@qq.com>
	 * @since 2021/6/7 13:21
	 */
	@ResponseBody
	@RequestMapping("/getAllProductClassId")
	public Result getAllProductClassId() {
		try {
			List<Map<String,Object>> productClassIdList = productClassService.getAllProductClassId();
			return new Result(200, MessageConstant.GET_ALL_PRODUCTCLASSID_SUCCESS, productClassIdList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(500, MessageConstant.GET_ALL_PRODUCTCLASSID_FAIL, null);
		}
		
	}
	
	
	/**
	 * 查询商品细分类列表功能
	 *
	 * @param queryPageBean 分页查询类
	 * @return com.sword.page.PageResult
	 * @author <1254087415@qq.com>
	 * @since 2021/6/4 0:35
	 */
	@RequestMapping("/queryProductList")
	@ResponseBody
	public PageResult queryProductList(@RequestBody QueryPageBean queryPageBean) {
		try {
			return productListService.queryProductUnit(queryPageBean);
		} catch (Exception e) {
			e.printStackTrace();
			return new PageResult(500, MessageConstant.QUERY_PRODOCTLIST_FAIL, null, null);
		}
	}
	
	/**
	 * 添加商品细分类功能
	 *
	 * @param productlist 商品细分类数据
	 * @return com.sword.page.Result
	 * @author <1254087415@qq.com>
	 * @since 2021/6/5 11:53
	 */
	@ResponseBody
	@RequestMapping("/addProductList")
	public Result addProductList(@RequestBody Productlist productlist) {
		try {
			// 获取session保存进数据
			String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
			productlist.setEmployeeid(userId);
			
			// 判断是否添加成功
			Integer flag = productListService.addProductList(productlist);
			if (flag <= 0) {
				return new Result(500, MessageConstant.ADD_PRODOCTLIST_FAIL, null);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(500, MessageConstant.ADD_PRODOCTLIST_FAIL, null);
		}
		return new Result(200, MessageConstant.ADD_PRODOCTLIST_SUCCESS, null);
	}
	
	/**
	 * 编辑商品细分类功能
	 *
	 * @param productlist 商品细分类数据
	 * @return com.sword.page.Result
	 * @author <1254087415@qq.com>
	 * @since 2021/6/5 13:28
	 */
	@ResponseBody
	@RequestMapping("/editProductList")
	public Result editProductList(@RequestBody Productlist productlist) {
		try {
			Integer flag = productListService.editProductList(productlist);
			if (flag <= 0) {
				return new Result(500, MessageConstant.EDIT_PRODOCTLIST_FAIL, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(500, MessageConstant.EDIT_PRODOCTLIST_FAIL, null);
		}
		return new Result(200, MessageConstant.EDIT_PRODOCTLIST_SUCCESS, null);
	}
	
	/**
	 * 删除商品细分类功能
	 *
	 * @param id 商品细分类id
	 * @return com.sword.page.Result
	 * @author <1254087415@qq.com>
	 * @since 2021/6/5 13:36
	 */
	@ResponseBody
	@RequestMapping("deleteProductList")
	public Result deleteProductList(Integer id) {
		try {
			Integer flag = productListService.deleteProductList(id);
			if (flag <= 0) {
				return new Result(500, MessageConstant.DELETE_PRODOCTLIST_FAIL, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(500, MessageConstant.DELETE_PRODOCTLIST_FAIL, null);
		}
		return new Result(200, MessageConstant.DELETE_PRODOCTLIST_SUCCESS, null);
	}
	
	
	/**
	 * 查询商品计量单位列表功能
	 *
	 * @param queryPageBean 分页查询类
	 * @return com.sword.page.PageResult
	 * @author <1254087415@qq.com>
	 * @since 2021/6/4 0:35
	 */
	@RequestMapping("/queryProductUnit")
	@ResponseBody
	public PageResult queryProductUnit(@RequestBody QueryPageBean queryPageBean) {
		try {
			return productUnitService.queryProductUnit(queryPageBean);
		} catch (Exception e) {
			e.printStackTrace();
			return new PageResult(500, MessageConstant.QUERY_PRODOCTUNIT_FAIL, null, null);
		}
	}
	
	/**
	 * 添加商品计量单位功能
	 *
	 * @param productunit 商品计量单位数据
	 * @return com.sword.page.Result
	 * @author <1254087415@qq.com>
	 * @since 2021/6/5 10:01
	 */
	@ResponseBody
	@RequestMapping("/addProductUnit")
	public Result addProductUnit(@RequestBody Productunit productunit) {
		try {
			// 获取session保存进数据
			String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
			productunit.setEmployeeid(userId);
			Integer flag = productUnitService.addProductUnit(productunit);
			
			if (flag == 0) {
				return new Result(500, MessageConstant.ADD_PRODOCTUNIT_FAIL, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(500, MessageConstant.ADD_PRODOCTUNIT_FAIL, null);
		}
		return new Result(200, MessageConstant.ADD_PRODOCTUNIT_SUCCESS, null);
	}
	
	/**
	 * 编辑商品计量单位功能
	 *
	 * @param productunit 商品计量单位查询
	 * @return com.sword.page.Result
	 * @author <1254087415@qq.com>
	 * @since 2021/6/5 14:14
	 */
	@ResponseBody
	@RequestMapping("editProductUnit")
	public Result editProductUnit(@RequestBody Productunit productunit) {
		try {
			Integer flag = productUnitService.editProductUnit(productunit);
			if (flag <= 0) {
				return new Result(500, MessageConstant.EDIT_PRODOCTUNIT_FAIL, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(500, MessageConstant.EDIT_PRODOCTUNIT_FAIL, null);
		}
		return new Result(200, MessageConstant.EDIT_PRODOCTUNIT_SUCCESS, null);
	}
	
	/**
	 * 删除商品计量单位功能
	 *
	 * @param id 商品计量单位id
	 * @return com.sword.page.Result
	 * @author <1254087415@qq.com>
	 * @since 2021/6/5 14:23
	 */
	@ResponseBody
	@RequestMapping("deleteProductUnit")
	public Result deleteProductUnit(Integer id) {
		try {
			Integer flag = productUnitService.deleteProductUnit(id);
			if (flag <= 0) {
				return new Result(500, MessageConstant.DELETE_PRODOCTUNIT_FAIL, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(500, MessageConstant.DELETE_PRODOCTUNIT_FAIL, null);
		}
		
		return new Result(200, MessageConstant.DELETE_PRODOCTUNIT_SUCCESS, null);
	}
	
}
